package br.edu.utfpr.db;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author André Luis Schwerz
 */
@WebServlet(name = "DatabaseManagerServlet", urlPatterns = {"/DatabaseManagerServlet"})
public class DatabaseManagerServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, FileNotFoundException {

        DatabaseManager.HOST = request.getParameter("host");
        DatabaseManager.PORT = request.getParameter("port");
        DatabaseManager.LOGIN = request.getParameter("login");
        DatabaseManager.PASSWORD = request.getParameter("password");
        DatabaseManager.DATABASE = request.getParameter("database");

        boolean flag = false;
        try {
            flag = DatabaseManager.execute(getServletContext().getRealPath("include/script.sql"));
            if (flag) {
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<head>");
                out.println("</head>");
                out.println("<body>");
                out.println("<p> Banco criado com sucesso!</p>");
                out.println("</body>");
                out.println("</html>");
            }
        } catch (SQLException ex) {
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<head>");
                out.println("</head>");
                out.println("<body>");
                out.println("<p> PROBLEMA... Dados incorretos!!</p>");
                out.println("</body>");
                out.println("</html>");
        }
    }
}
